package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.comment.domain.ItemShopComment;
import cn.wolfcode.luowowo.comment.query.IteamShopCommentQuery;
import cn.wolfcode.luowowo.comment.service.IIteamShopCommentService;
import cn.wolfcode.luowowo.common.exception.LogistException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.IteamShop;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.server.IIteamShopService;
import cn.wolfcode.luowowo.member.server.IUserInfoService;
import cn.wolfcode.luowowo.member.server.IUserIteamService;
import cn.wolfcode.luowowo.website.anno.UserParm;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("itemshop")
public class IteamshopController {
    @Reference
    private IIteamShopService shopService;
    @Reference
    private IUserIteamService userIteamService;
    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private IIteamShopCommentService iIteamShopCommentService;

    @RequestMapping("")
    public String integral(Model model ,@UserParm UserInfo userInfo) {
        Long gold = userInfoService.selectgold(userInfo.getId());
        userInfo.setGold(gold);
        //用户信息
        model.addAttribute("userInfo", userInfo);
        //查询评论信息
        IteamShopCommentQuery query = new IteamShopCommentQuery();
        query.setPageSize(Integer.MAX_VALUE);
        Page page = iIteamShopCommentService.query(query);
        model.addAttribute("list",page.getContent());
        return "itemshop/list";
    }

    @RequestMapping("/itemTempl")
    public String itemTempl(Model model) {
        //所有道具
        List<IteamShop> iteamShops = shopService.selectAll();
        model.addAttribute("iteamShops", iteamShops);
        return "itemshop/itemTempl";
    }

    @RequestMapping("/myItemTempl")
    public String myItemTempl(Model model, @UserParm UserInfo userInfo) {

        //我的道具
        List<IteamShop> myIteams = shopService.selectMyIteams(userInfo.getId());
        model.addAttribute("myIteams", myIteams);
        return "itemshop/myItemTempl";
    }

    //购买道具
    @RequestMapping("/buy")
    @ResponseBody
    public Object buy(@UserParm UserInfo userInfo, Long id) {

        try {
            userIteamService.buyiteam(userInfo.getId(), id);
        } catch (LogistException le) {
            le.printStackTrace();
            return new AjaxResult(false, le.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "服务器繁忙!");
        }
        return AjaxResult.SUCCESS.addData("购买成功!");
    }
    //道具点评
    @RequestMapping("/commentAdd")
    public Object commentAdd(@UserParm UserInfo userInfo, ItemShopComment itemShopComment, Model model){
        if (userInfo==null){
            throw new LogistException("请登录");
        }
        itemShopComment.setUserId(userInfo.getId());
        itemShopComment.setHeadUrl(userInfo.getHeadImgUrl());
        itemShopComment.setUsername(userInfo.getNickname());
        itemShopComment.setLevel(userInfo.getLevel());

        itemShopComment = iIteamShopCommentService.save(itemShopComment);
        model.addAttribute("comment",itemShopComment);

        return "itemshop/commentTempl";
    }
}
